import React, {Component} from 'react';
import {getUserInfo, setUserEmail, getUserSignLog, setUserIsAutoStatus, logout} from '../../../api/user';
import {
    ScrollView,
    SafeAreaView,
    View,
    Text,
    StyleSheet,
    Dimensions,
    Switch,
    TouchableOpacity,
    TextInput,
    Alert,
    ActivityIndicator,
} from 'react-native';
import {Loading} from '../../../../src/utils/Loading';
import {Toast} from '../../../../src/utils/Toast';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import {Set_Token} from '../../../store/actions/user';
import {removeToken} from '../../../utils/Auth';
import {connect} from 'react-redux';

const width = Dimensions.get('window').width;
const height = Dimensions.get('window').height;

class UserInfoScreen extends Component {

    constructor() {
        super();
        this.state = {
            isEnabled: false,
            email: null,
            username: 'admin',
            userSign: null,
            signLogListData: [],
        };
    }

    componentDidMount() {
        //拉取用户信息
        // this.getUser();
        //this.getSignLog();
    }

    //获取用户信息
    // getUser = () => {
    //     Loading.show()
    //     getUserInfo(null).then(res => {
    //         console.log("getUserInfo", res)
    //         let {code, user, userSign} = res;
    //         if (code == 0) {
    //
    //             this.setState({
    //                 username: user.username,
    //                 email: user.email,
    //                 isEnabled: userSign.isAuto == 1 ? true : false,
    //             })
    //             Loading.hidden();
    //         }
    //     }).catch(err => {
    //         Loading.hidden();
    //         //debugger;
    //         // console.log("getUserInfo err", err)
    //     })
    // }
    //退出按钮
    btnLogout = () => {
        Alert.alert('退出账号', '确定退出当前账号', [{
            text: '取消', onPress: () => {
            },
        }, {
            text: '确定', onPress: () => {
                Loading.show();
                logout().then(res => {
                    if (res.code == 0) {
                        removeToken();//清除token
                        this.props.logout();//清除状态
                        Loading.hidden();
                    }

                }).catch(err => {
                    Toast.showWarning(err.toString())
                    Loading.hidden();
                }).finally(()=>{
                    Loading.hidden();
                });
            },
        }]);

    };

    render() {

        return (

            <SafeAreaView style={{flex: 1, backgroundColor: '#FFFF'}}>
                <View style={style.container}>
                    {/*<ActivityIndicator*/}
                    {/*    animating={this.state.Myanimating}*/}
                    {/*    color='#bc2b78'*/}
                    {/*    size="large"*/}
                    {/*    style={{}}*/}
                    {/*/>*/}
                    <View style={style.con_top}>
                        <Text style={{fontSize: 18}}>{this.state.username}</Text>
                        <TouchableOpacity onPress={this.btnLogout}>
                            <MaterialCommunityIcons size={17} name={'logout'}></MaterialCommunityIcons>
                        </TouchableOpacity>
                    </View>
                    <ScrollView>
                    </ScrollView>
                </View>

            </SafeAreaView>
        );
    }

};

const style = StyleSheet.create({
    container: {
        backgroundColor: '#F1F0F5',
        //backgroundColor:"#000",
        flex: 1,
        alignItems: 'center',
        //justifyContent: "center",
    },
    con_top: {
        backgroundColor: '#fff',
        borderBottomWidth: 1,
        borderStyle: 'solid',
        borderColor: '#E8E8EA',
        width: width,
        padding: '4%',
        // marginBottom: 8,
        flexDirection: 'row',
        justifyContent: 'space-between',
    },
    card: {
        backgroundColor: '#FFFEFE',
        width: width - 10 - 10,
        padding: 15,
        borderRadius: 6,
        marginTop: 8,
        //marginTop:5,

    },
    card_top: {
        paddingBottom: 15,
    },
    card_top_text: {
        fontSize: 18,
        fontWeight: 'bold',
    },
    card_container: {
        flexDirection: 'row',
        // flex:0,
        //alignItems: "space-between",
        justifyContent: 'space-between',

    },
    card_con_left: {
        flexDirection: 'row',
        justifyContent: 'space-between',
    },
    cd_con_left_user: {
        borderRadius: 30,
        height: 60,
        width: 60,
        backgroundColor: '#409EFF',
        justifyContent: 'center',
        alignItems: 'center',
        marginRight: 15,
    },
    cd_con_left_ringt: {
        height: 60,
        justifyContent: 'space-around',
    },
    cd_con_left_ringt_top: {},

    cd_con_left_ringt_btm: {
        width: 60,
        height: 30,
        backgroundColor: '#FDF0F0',

        justifyContent: 'center',
        alignItems: 'center',
    },
    cd_con_left_ringt_btm2: {
        //padding:10,
        width: 60,
        height: 30,
        backgroundColor: '#F1F9EC',
        justifyContent: 'center',
        alignItems: 'center',
    },
    card_con_right: {
        justifyContent: 'center',
        alignItems: 'center',
    },


    //邮箱卡片部分
    card_setEmail: {},
    card_setEmail_top: {
        flexDirection: 'row',
        //justifyContent: "space-between",
        marginTop: 15,
    },
    card_setEmail_left: {
        alignItems: 'center',
        justifyContent: 'center',
        //backgroundColor:"yellow",
        marginRight: 5,
    },
    card_setEmail_right: {
        // backgroundColor:"red",
        alignItems: 'center',
        justifyContent: 'center',
        //marginRight: 5
    },
    card_setEmail_btm: {
        marginTop: 15,
    },
    card_setEmail_btn: {
        borderRadius: 30,
        backgroundColor: '#409EFF',
        height: 35,
        justifyContent: 'center',
        alignItems: 'center',
    },


    //打卡记录部分
    card_tab_container: {},

    cd_con_table_top: {
        marginBottom: 5,
        flexDirection: 'row',
        justifyContent: 'space-between',
    },
    cd_con_table_top_sj: {},
    cd_con_table_top_msg: {},
    cd_con_table_con: {
        marginTop: 2,

        flexDirection: 'row',
        justifyContent: 'space-between',
    },
    cd_con_table_con_sj: {},
    cd_con_table_con_msj: {},
});


const dispatchToProps = (dispatch) => {
    return {
        logout() {
            //清空状态
            let action = Set_Token(null);
            dispatch(action);
        },
    };
};

export default connect(null, dispatchToProps)(UserInfoScreen);

